const aliyun = 'http://bhyx-oss.oss-cn-hangzhou.aliyuncs.com/Guestfunds/'; //默认头像

const active ='http://bhyx-oss.oss-cn-hangzhou.aliyuncs.com/Guestfunds/logo.png'
// const hosturl = 'http://117.34.71.4:8081/';   //线上
const hosturl = 'https://api.beihunyouxuan.com/';   //线上
// const hosturl = 'http://192.168.0.11:8081/'; 	
const jwd = '109.06458,34.2727';   //线上
// let hosturl = 'http://htxtest.365vipbox.com/api/';  // 线下
const email = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; // 邮箱正则

const mobile = /^1\d{10}$/; // 正则判断手机号

// 以前const password = /^[A-Za-z0-9]{6,10}$/
// 修改 
const password = /^[A-Za-z0-9]{6,}$/;		// 正则判断密码（字母加数字）

const patt1 = new RegExp(/\s+/g); //正则验证空格


// 跳转子页面
const navigateTo = (opt) => {
	console.log(opt);
	uni.navigateTo({
		url: opt
	});
}

//打开主页面调用 
const switchTab = (opt) => {
	console.log(opt);
	uni.switchTab({
		url: opt,
	});
}

//关闭当前页面打开新的页面
const redirectTo = (opt) => {
	console.log(opt);
	uni.redirectTo({
		url: opt,
	});
}

//关闭所有页面打开新的页面
const reLaunch = (opt) => {
	console.log(opt);
	uni.reLaunch({
		url: opt,
	});
}

// 点击复制 app
const fuzhi = (value) => {
	var value = value.toString();
	uni.setClipboardData({
		data: value
	});
}

// 点击复制 H5
const fuzhis = (value) => {
	var value = value.toString();
	console.log(value)
	var tt = this
	var val = value;
	let result
	let textarea = document.createElement("textarea")
	textarea.value = val
	textarea.readOnly = "readOnly"
	document.body.appendChild(textarea)
	textarea.select() // 选中文本内容
	textarea.setSelectionRange(0, val.length)
	result = document.execCommand("copy")
	textarea.remove()
	uni.showToast({
		title: "复制成功",
	})
	return result
}

//进行本地存储
const setStorage = (opt) => {
	uni.setStorage({
		key: opt.key,
		data: opt.data,
		success: function() {

		}
	});
}

// 获取本地存储
const setStorageSync = (opt) => {
	console.log(opt.key)
	uni.setStorageSync({
		key: opt.key,
		success: function(res) {
			console.log(res.data)
			opt.success(res.data);
		}
	});
}

// 长按查看和保存图片
const tupian = (e) => {
	console.log(e)
	var zhuanhua = e.split(","); // 在每个逗号(,)处进行分解  ["abc", "abcd", "aaa"]
	uni.previewImage({
		urls: zhuanhua,
		longPressActions: {
		  itemList: ['保存图片'],
		  success: (data) => {
		    uni.downloadFile({
		      url: zhuanhua[0],
		      success: (res) => {
		        console.log('downloadFile success, res is', res.tempFilePath)
		        uni.saveImageToPhotosAlbum({
		          filePath: res.tempFilePath, //不支持网络地址
		          success: function () {
					uni.showToast({
						title: '图片保存成功',
						icon: "none"
					});
		          }
		        });
		      },
		      fail: (err) => {
		        console.log('downloadFile fail, err is:', err)
		      }
		    })
		  },
		  fail: function(err) {
		    console.log(err.errMsg);
		  }
		}
	});
};
// 长按查看和保存图片多张
const tupian_s = (e,index) => {
	// console.log(e,"多张")
	console.log(index,"indexindex")
	uni.previewImage({
		urls: e,
		current: index,
		longPressActions: {
		  itemList: ['保存图片'],
		  success: (data) => {
		    uni.downloadFile({
		      url: e[index],
		      success: (res) => {
		        console.log('downloadFile success, res is', res.tempFilePath)
		        uni.saveImageToPhotosAlbum({
		          filePath: res.tempFilePath, //不支持网络地址
		          success: function () {
					uni.showToast({
						title: '图片保存成功',
						icon: "none"
					});
		          }
		        });
		      },
		      fail: (err) => {
		        console.log('downloadFile fail, err is:', err)
		      }
		    })
		  },
		  fail: function(err) {
		    console.log(err.errMsg);
		  }
		}
	});
};

//进行本地存储
const fenxaing = (opt) => {
	console.log('分享')
	console.log(opt)
	if(opt==true){
		uni.showShareMenu({
			withShareTicket:false,
			//设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
			menus:["shareAppMessage","shareTimeline"]
		})
	}else{
		uni.hideShareMenu({
			withShareTicket:false,
			//设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
			menus:["shareAppMessage","shareTimeline"]
		})
	}
	
}

const dizhi = (opt,adress) => {
	console.log(opt)
	console.log(adress)
	//    　　latitude: opt.split(',')[0],
	//    　　longitude: opt.split(',')[1],
	console.log('进入导航')
	//  经纬度暂时写死，后面介入数据
	let latitude = Number(opt.split(',')[0]);
	let longitude = Number(opt.split(',')[1]);
	let address='武汉xxx'
	// 获取定位信息
	uni.getLocation({
		type: 'wgs84', //返回可以用于uni.openLocation的经纬度
		// 用户允许获取定位
		success: function(res) {
			console.log(res, '经纬度===>')
			if (res.errMsg == "getLocation:ok") {
				console.log(latitude)
				console.log(longitude)
				uni.openLocation({
					// 传入你要去的纬度
					latitude: latitude,
					// 传入你要去的经度
					longitude: longitude,
					// 传入你要去的地址信息 不填则为空
					address: adress,
					// 缩放大小
					scale: 18,
					success: function() {
						console.log('成功的回调success');
					}
				});
			}
		},
		// 用户拒绝获取定位后 再次点击触发
		fail: function(res) {
			console.log(res)
			if (res.errMsg == "getLocation:fail auth deny") {
				uni.showModal({
					content: '检测到您没打开获取信息功能权限，是否去设置打开？',
					confirmText: "确认",
					cancelText: '取消',
					success: (res) => {
						if (res.confirm) {
							uni.openSetting({
								success: (res) => {
									console.log('确定');
								}
							})
						} else {
							console.log('取消');
							return false;
						}
					}
				})
			}
		}
	});

	
	
	// uni.openLocation({
	//    　　latitude: opt.split(',')[0], 
	//    　　longitude: opt.split(',')[1],
	//    　　name: adress,
	//    　　success() {
	//        　　console.log('success');
	//    　　}
	//    });
}

const phone = (num) => {
	uni.makePhoneCall({
		phoneNumber:num.toString(),//电话号码
		success:function(e){
			console.log(e);
		},
		fail:function(e){
			console.log(e);
		}
	})
}
const timestampToTime = (timestamp,state) => {
	let now = new Date(timestamp),
	  y = now.getFullYear(),
	  m = now.getMonth() + 1,
	  d = now.getDate();
	  if(state==0){
		  
	  }else{
		  return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0, 8);
	  }
	
	  
}



const s_dizhi = (value) => {
	let str = uni.getStorageSync('shopaddress');	let reg = /.+?(省|市|自治区|自治州|县|区)/g;	let bb = str.match(reg);	if (bb[1] != undefined && bb[2] != undefined) {		return  bb[0] + '-' + bb[1] + '-' + bb[2];	} else if (bb[1] != undefined && bb[2] == undefined) {		return bb[0] + bb[1] + '-' + '全部';	} else if (bb[1] == undefined && bb[2] == undefined) {		return bb[0] + '-' + '全部' + '-' + '全部';	}
}

export default {
	hosturl,        //网络请求
	navigateTo,     //跳转子页面调用
	switchTab,      //打开tab页面
	fuzhi,          //点击复制
	fuzhis,         //点击复制
	setStorage,     //进行本地存储
	setStorageSync, //获取本地存储
	tupian,         //点击查看图片
	tupian_s,		//查看多张图片
	aliyun,         //阿里云地址
	redirectTo,     //关闭当前页面打开新的页面
	reLaunch,       //关闭所有页面打开新的页面
	email,          //邮箱正则
	password,      //密码正则
	patt1,          //空格正则
	mobile,          //手机号正则
	fenxaing,
	jwd,         //经纬度
	dizhi,
	phone,
	active,      //默认头像
	timestampToTime,    //时间戳
	s_dizhi
	
}
